package com.ideaaedi.springcloud.jd.user.controller.sys;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.ideaaedi.springcloud.jd.commonds.entity.PageInfo;
import com.ideaaedi.springcloud.jd.commonds.entity.Result;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysGroupAddReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysGroupBindUserReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysGroupListReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysGroupQueryUserReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysGroupUpdateReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysGroupDetailRespVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysGroupListRespVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysUserListRespVO;
import com.ideaaedi.springcloud.jd.user.service.SysGroupService;
import com.ideaaedi.springcloud.jd.user.service.SysUserGroupReService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 系统-分组
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img
 * src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 2021.0.5.B
 */
@Validated
@RestController
@ApiSort(value = 11)
@RequestMapping("/sys-group")
@Api(value = "系统-分组", tags = {"系统-分组"})
public class SysGroupController {
    
    @Resource
    private SysGroupService sysGroupService;
    
    @Resource
    private SysUserGroupReService sysUserGroupReService;
    
    /**
     * 增
     *
     * @param req 参数
     *
     * @return 新增的数据详情
     */
    @ApiOperation("增")
    @PostMapping("/add")
    @ApiOperationSupport(order = 1)
    public Result<SysGroupDetailRespVO> add(@RequestBody @Validated SysGroupAddReqVO req) {
        return Result.success(sysGroupService.add(req, true));
    }
    
    /**
     * 删
     *
     * @param id 要删除数据的id
     *
     * @return 删除了的数据详情
     */
    @ApiOperation("删")
    @DeleteMapping("/delete/{id}")
    @ApiOperationSupport(order = 2)
    public Result<SysGroupDetailRespVO> delete(@ApiParam(value = "要删除数据的id", required = true)
                                               @PathVariable("id") @NotNull(message = "id cannot be null.") Long id) {
        return Result.success(sysGroupService.delete(id));
    }
    
    /**
     * 改
     *
     * @param req 参数
     *
     * @return 修改后的数据详情
     */
    @ApiOperation("改")
    @PutMapping("/update")
    @ApiOperationSupport(order = 3)
    public Result<SysGroupDetailRespVO> update(@RequestBody @Validated SysGroupUpdateReqVO req) {
        return Result.success(sysGroupService.update(req));
    }
    
    /**
     * 查列表
     *
     * @param req 参数
     *
     * @return 数据列表
     */
    @PostMapping("/list")
    @ApiOperation("查列表")
    @ApiOperationSupport(order = 4)
    public Result<PageInfo<SysGroupListRespVO>> list(@RequestBody @Validated SysGroupListReqVO req) {
        return Result.success(sysGroupService.list(req));
    }
    
    /**
     * 查详情
     *
     * @param id 要查询数据的id
     *
     * @return 数据详情
     */
    @ApiOperation("查详情")
    @GetMapping("/detail/{id}")
    @ApiOperationSupport(order = 5)
    public Result<SysGroupDetailRespVO> detail(@ApiParam(value = "要查询数据的id", required = true)
                                               @PathVariable("id") @NotNull(message = "id cannot be null.") Long id) {
        return Result.success(sysGroupService.detail(id));
    }
    
    /**
     * 给分组绑定用户
     *
     * @param req 请求模型
     *
     * @return 分组拥有的用户
     */
    @PostMapping("/bind-user")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "给分组绑定用户")
    public Result<List<SysUserListRespVO>> bindUser(@Validated @RequestBody SysGroupBindUserReqVO req) {
        return Result.success(sysUserGroupReService.bindUser(req));
    }
    
    /**
     * 查询分组拥有的用户
     *
     * @param req 请求参数
     *
     * @return 分组拥有的用户
     */
    @PostMapping("/query-user")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "查询分组拥有的用户")
    public Result<List<SysUserListRespVO>> queryUser(@Validated @RequestBody SysGroupQueryUserReqVO req) {
        return Result.success(sysUserGroupReService.queryUser(req));
    }
}
